package org.hxd.client.order.service.impl;

import org.hxd.client.order.dao.OrderDao;
import org.hxd.client.order.service.OrderService;
import org.hxd.pojo.Orderitem;
import org.hxd.pojo.Orders;
import org.hxd.pojo.Products;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    OrderDao orderDao;

    @Override
    public void createOrder(Orders orders, Map<Products, Integer> cart) {
        Orderitem orderitem = new Orderitem();
        for (Products p:cart.keySet()) {
            orderitem.setProducts(p);
            orderitem.setOrders(orders);
            orderitem.setBuynum(cart.get(p));
            //插入订单项
            orderDao.insertOrderitem(orderitem);
            //修改商品库存
            orderDao.ModifyProductNum(orderitem);
        }
        //测试异常处理
//        if(1 == 1){
//            int i = 1 / 0;
//        }
        orderDao.createOrder(orders);
    }

    @Override
    public void paySuccess(String order_id) {
        orderDao.paySuccess(order_id);
    }
}
